import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../config/service_method.dart';
import '../model/login.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../routers/application.dart';
import 'package:flutter/services.dart';
import 'dart:convert';
import '../common/shared_preferences.dart';
import '../common/ok_toast.dart';
import '../common/j_push.dart';

class LoginPage extends StatefulWidget{
    @override
    _LoginPageState createState() => _LoginPageState();
}

// class _LoginPageState extends State<LoginPage>{
    
//     Widget build(BuildContext context){
//         return Text("hhh");
//     }

// }
class _LoginPageState extends State<LoginPage>{

    TextEditingController _userName = TextEditingController();
    TextEditingController _passWord = TextEditingController();


    Future<bool> _onWillPop() {
        return showDialog(
            context: context,
            builder: (context) => new AlertDialog(
                title: new Text('退出App?'),
                // content: new Text('Do you want to exit an App'),
                actions: <Widget>[
                    new FlatButton(
                        onPressed: () => Navigator.of(context).pop(false),
                        child: new Text('不'),
                    ),
                    new FlatButton(
                        onPressed: () async {
                            await pop();
                        },
                        child: new Text('是的'),
                    ),
                ],
            ),
        ) ?? false;
    }

    static Future<void> pop() async {
        await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
    }


    @override
    Widget build(BuildContext context){

        return WillPopScope(
            onWillPop: _onWillPop,
            child:Container(
                child: Scaffold(
                    body: Container(
                        alignment: Alignment.center,
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                                Expanded(
                                    child: Container(
                                        alignment: Alignment.bottomCenter,
                                        child: Image.asset(
                                            'image/logo.png',
                                            scale:1.5,
                                        ),
                                    )
                                ),
                                _loginInput(),
                                Expanded(
                                    child: Container(
                                        alignment: Alignment.bottomCenter,
                                        padding: EdgeInsets.only(bottom: 20.0),
                                        child:  Text("登陆即代表阅读并同意服务协议"),
                                    ),
                                )
                                
                            ],
                        ),
                    ),
                ),
            )
        );

        
    }

    Widget _loginInput(){
        return Container(
            padding: EdgeInsets.fromLTRB(20.0, 0, 20.0, 0),
            child: Column(
                children: <Widget>[
                    TextField(
                        controller: _userName,
                        decoration: InputDecoration(
                            labelText: "用户名",
                            // hintText: "请输入用户名",
                            prefixIcon: Icon(Icons.person)
                        ),
                    ),
                    TextField(
                        controller: _passWord,
                        obscureText:true,
                        decoration: InputDecoration(
                            labelText: "密码",
                            // hintText: "请输入密码",
                            prefixIcon: Icon(Icons.lock)
                        ),
                    ),
                    
                    Container(
                        width: double.infinity,
                        height: ScreenUtil().setHeight(80),
                        margin: EdgeInsets.all(20.0),
                        child: FlatButton(
                            color: Color.fromRGBO(81, 126, 219, 1),
                            shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                            child: Text(
                                "登录",
                                style: TextStyle(color: Colors.white),
                            ),
                            // onPressed:(){},
                             onPressed: _dologin,
                        )
                    ),
                ],
            )
        );
        
        
    }

    void _dologin()async{

        
       
        JPushInit.initPlatformState();
        JPushInit.jPushID();
        return;

        


        if(_userName.text == "" || _passWord.text == ""){
            OkToast.singleToast("请完善账号密码后再登陆");
            return;
        }
        var data = {
            "loginId": _userName.text,
            "loginPwd": _passWord.text,
        };
        
        await DioUtil.request(context,'/tqes/web/login/check',formData: data).then((val){
            Map<String,dynamic> responseData = json.decode(val.toString());
            //登录成功
            if(responseData["success"]){
                try{
                    LoginModel loginData = LoginModel.fromJson(val.data);
                    SharedPreferencesStorea.setValue("loginToken",loginData.data.authorization);
                    SharedPreferencesStorea.setValue("roleId",loginData.data.sysRole.roleId);
                    SharedPreferencesStorea.setValue("userName",loginData.data.userName);
                    SharedPreferencesStorea.setValue("userCode",loginData.data.userCode);
                    Application.router.navigateTo(context,"/home");
                }catch(e){
                    print("json解析出现错误${e}");
                    return;
                }
            }else{
                //登陆失败
                OkToast.singleToast(responseData["msg"]);
                return;
            }

        });
    }
}